module("luci.controller.netflow", package.seeall)

function index()
    print("Registering NetFlow menu")  -- 添加调试信息
    entry({"admin", "network", "netflow"}, firstchild(), _("NetFlow"), 60).dependent=false
    print("NetFlow menu registered")  -- 添加调试信息
    entry({"admin", "network", "netflow", "overview"}, template("netflow/view_overview"), _("系统概览"), 10)
    entry({"admin", "network", "netflow", "statistics"}, template("netflow/view_statistics"), _("统计信息"), 20)
    entry({"admin", "network", "netflow", "status"}, call("action_status"), nil)
    entry({"admin", "network", "netflow", "metrics"}, call("action_metrics"), nil)
    entry({"admin", "network", "netflow", "app_stats"}, call("action_app_stats"), nil)
    entry({"admin", "network", "netflow", "flow_stats"}, call("action_flow_stats"), nil)
end

function action_status()
    local result = {
        running = true,
        version = "1.0.5",
        uptime = "5天 3小时 27分钟"
    }
    luci.http.prepare_content("application/json")
    luci.http.write_json(result)
end

function action_metrics()
    local result = {
        load_factor = 0.65,
        congestion_index = 0.12,
        traffic = {
            current_bps = 65000000,
            capacity_bps = 100000000
        },
        bandwidth_allocation = {
            rt_min = 40,
            rt_max = 100,
            streaming_min = 30,
            streaming_max = 80,
            normal_min = 20,
            normal_max = 60,
            background_min = 10,
            background_max = 40
        }
    }
    luci.http.prepare_content("application/json")
    luci.http.write_json(result)
end

function action_app_stats()
    local result = {
        app_traffic = {
            video_conference = 1250,
            voip = 850,
            gaming = 750,
            video_streaming = 2500,
            audio_streaming = 1200,
            web_browsing = 1800,
            file_download = 1500,
            p2p = 950,
            other = 650
        }
    }
    luci.http.prepare_content("application/json")
    luci.http.write_json(result)
end

function action_flow_stats()
    local hourly = {}
    local now = os.time()
    
    for i = 23, 0, -1 do
        local time = os.date("%H:00", now - i * 3600)
        local up_bytes = math.random(1073741824, 5368709120) -- 1GB-5GB
        local down_bytes = math.random(2147483648, 10737418240) -- 2GB-10GB
        
        table.insert(hourly, {
            time = time,
            up_bytes = up_bytes,
            down_bytes = down_bytes
        })
    end
    
    local result = {
        hourly = hourly,
        app_traffic = {
            [1] = 1250000000, -- 视频会议
            [2] = 850000000,  -- 语音通话
            [3] = 750000000,  -- 在线游戏
            [4] = 3500000000, -- 视频流媒体
            [5] = 1200000000, -- 音频流媒体
            [6] = 1800000000, -- 网页浏览
            [7] = 2500000000, -- 文件下载
            [8] = 950000000,  -- P2P下载
            [9] = 350000000,  -- 远程登录
            [10] = 450000000, -- 邮件
            [11] = 650000000, -- 系统更新
            [12] = 750000000  -- 其他
        },
        queue_stats = {
            [1] = {packets = 1250000, bytes = 1250000000},
            [2] = {packets = 2350000, bytes = 2350000000},
            [3] = {packets = 3150000, bytes = 3150000000},
            [4] = {packets = 1850000, bytes = 1850000000}
        },
        drop_stats = {
            [1] = {drops = 1250, rate = 0.001},
            [2] = {drops = 3500, rate = 0.005},
            [3] = {drops = 8500, rate = 0.012},
            [4] = {drops = 15000, rate = 0.028}
        }
    }
    
    luci.http.prepare_content("application/json")
    luci.http.write_json(result)
end
